from django.db import models
import uuid
import re
# Create your models here.
"""
Django 数据库迁移命令: 
python manage.py makemigrations
python manage.py migrate
.venv\Scripts\activate
"""

# 项目状态
def project_status_default():
    return [
        (1, '未开始'),
        (2, '进行中'),
        (3, '已完成'),
        (4, '已取消'),
        (5, '已暂停'),
    ]

class User(models.Model):
    user_id = models.CharField(max_length=255, primary_key=True)
    # 用户名
    user_name = models.CharField(max_length=255, null=True, blank=True, default='黑寡妇用户')
    # 用户账号
    account = models.CharField(max_length=255)
    # 用户密码
    password = models.CharField(max_length=255)
    # 创建时间
    create_time = models.DateTimeField(auto_now_add=True)
    # 更新时间
    update_time = models.DateTimeField(auto_now=True)
    def save(self, *args, **kwargs):
        if not self.user_id:
            self.user_id = str(uuid.uuid4())
        # 用户密码只能存在6-12位,并且只能是大小写英文、数字、@符号
        if not re.match(r'^[a-zA-Z0-9@]{6,12}$', self.password):
            raise ValueError("用户密码只能存在6-12位,并且只能是大小写英文、数字、@符号")
        # 用户账号只能是6-11位数，只能是数字
        if not re.match(r'^\d{6,11}$', self.account):
            raise ValueError("用户账号只能是6-11位数，只能是数字")
        super().save(*args, **kwargs)


# 项目
class Project(models.Model):
    project_id = models.CharField(max_length=255, primary_key=True)
    # 项目名称
    project_name = models.CharField(max_length=255, unique=True)
    # 项目描述
    project_description = models.TextField(null=True, blank=True)
    # 项目状态
    project_status_choice = [
        (1, '未开始'),
        (2, '进行中'),
        (3, '已完成'),
        (4, '已取消'),
        (5, '已暂停'),
    ]
    # 项目状态
    project_status = models.IntegerField(choices=project_status_choice, default=1)
    # 项目负责人
    project_leader = models.ForeignKey(User, on_delete=models.CASCADE)
    # 项目文档地址
    project_doc_url = models.CharField(max_length=255, null=True, blank=True)
    # 创建时间
    create_time = models.DateTimeField(auto_now_add=True)
    # 更新时间
    update_time = models.DateTimeField(auto_now=True)
    def save(self, *args, **kwargs):
        if not self.project_id:
            self.project_id = str(uuid.uuid4())
        
        super().save(*args, **kwargs)


# 项目子计划
class ProjectSubPlan(models.Model):
    sub_plan_id = models.CharField(max_length=255, primary_key=True)
    # 项目
    project = models.ForeignKey(Project, on_delete=models.CASCADE)
    # 子计划名称
    sub_plan_name = models.CharField(max_length=255)
    # 子计划描述
    sub_plan_description = models.TextField(null=True, blank=True)
    # 子计划总负责人
    sub_plan_total_leader = models.ForeignKey(User, on_delete=models.CASCADE)
    # 子计划状态
    sub_plan_status_choice = project_status_default()
    # 子计划状态
    sub_plan_status = models.IntegerField(choices=sub_plan_status_choice, default=1)
    # 创建时间
    create_time = models.DateTimeField(auto_now_add=True)
    # 更新时间
    update_time = models.DateTimeField(auto_now=True)
    def save(self, *args, **kwargs):
        if not self.sub_plan_id:
            self.sub_plan_id = str(uuid.uuid4())
        super().save(*args, **kwargs)

# 项目原型图
class ProjectPrototype(models.Model):
    prototype_id = models.CharField(max_length=255, primary_key=True)
    # 项目
    project = models.ForeignKey(Project, on_delete=models.CASCADE)
    # 原型图名称
    prototype_name = models.CharField(max_length=255, null=True, blank=True)
    # 原型图描述
    prototype_description = models.TextField(null=True, blank=True)
    # 原型图地址
    prototype_url = models.FileField(upload_to='project/prototype/')
    # 创建时间
    create_time = models.DateTimeField(auto_now_add=True)
    # 更新时间
    update_time = models.DateTimeField(auto_now=True)
    def save(self, *args, **kwargs):
        if not self.prototype_id:
            self.prototype_id = str(uuid.uuid4())
        super().save(*args, **kwargs)



# 项目负责人
class ProjectLeader(models.Model):
    leader_id = models.CharField(max_length=255, primary_key=True)
    # 项目
    project = models.ForeignKey(Project, on_delete=models.CASCADE)
    # 子计划项目
    sub_plan_project = models.ForeignKey(ProjectSubPlan, on_delete=models.CASCADE)
    # 负责人
    leader_user = models.ForeignKey(User, on_delete=models.CASCADE)
    # 备注
    leader_remark = models.TextField(null=True, blank=True)
    # 创建时间
    create_time = models.DateTimeField(auto_now_add=True)
    # 更新时间
    update_time = models.DateTimeField(auto_now=True)
    def save(self, *args, **kwargs):
        if not self.leader_id:
            self.leader_id = str(uuid.uuid4())
        super().save(*args, **kwargs)
